package com.examples.gateway.helper.config;


import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.compress.utils.Lists;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Dai YaLu
 */
@Getter
@Setter
@RefreshScope
@ConfigurationProperties(prefix = GatewayHelperProperties.PREFIX)
public class GatewayHelperProperties {

    public static final String PREFIX = "exam.gateway.helper";

    private ReWriteBody reWriteBody = new ReWriteBody();

    /**
     * 重写请求ContentType类型为 application/json 的请求体内容
     */
    private boolean reWriteReqJsonBody = true;

    private Permission permission = new Permission();

    private TokenResolver tokenResolver = new TokenResolver();

    private ApiReplay apiReplay = new ApiReplay();

    /**
     * 流量重放校验
     */
    @Getter
    @Setter
    public static class ApiReplay {

        private boolean enabled = false;

        /**
         * 请求ID对应的key,从header中获取
         */
        private String requestIdKey = "EXAM_REQ_ID";

        /**
         * 忽略的Path
         */
        private List<String> skipPaths = new ArrayList<>();
    }

    /**
     * Token解析规则
     */
    @Getter
    @Setter
    public static class TokenResolver {

        private String headerKey = "Authorization";

        private String cookieKey = "examToken";

        private String queryKey = "examToken";

        private String prefix = "Bearer ";

        private boolean enableCheckTokenTamper = false;

        private String validateKey = "_exam";

        private String oAuthService = "examples-oauth-service";

        private String getUserDetailPath = "/token/checkToken";

    }

    /**
     * 权限
     */
    @Getter
    @Setter
    public static class Permission {

        private boolean enabled = true;

        /**
         * 跳过的路径
         */
        private List<String> skipPaths = new ArrayList<>();

        /**
         * 内部的路径
         */
        private List<String> internalPaths = new ArrayList<>();

    }

    @Data
    public static class ReWriteBody{

        private List<String> request = Lists.newArrayList();

        private List<String> response = Lists.newArrayList();
    }

}
